<?php
namespace App\Model;

use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class National extends BaseModel
{
    protected $table = 'national_gift_log';//国庆送礼记录表
    protected $send_gift_level_log = 'national_send_log';//送礼达到阶层的记录表
    protected $gift_table = 'gift_library';//礼品信息
    protected $connection = 'api';//数据库地址
    protected $activityConnection = 'activity';//活动数据库

    /**
     * 国庆礼物统计
     * */
   public function getGiftLog(){
        $res = DB::connection($this->activityConnection)->table($this->table)->select(DB::raw('SUM(send_nums) as all_send_nums,COUNT(DISTINCT user_id) as send_user_nums,SUM(diamon_value * send_nums) as all_diamon,MAX(created_at) as created_at,gift_id'))->where('gift_id','!=',0)->groupBy('gift_id')->get();
        $res = objToArray($res);
        return $res;
   }
   /**
    * 获取商品名称
    */
   public function getGiftInfo($goodsId)
   {
       $res = DB::connection($this->connection)->table($this->gift_table)->where('gift_id',$goodsId)->first();
       $res = objToArray($res);
       return $res;
   }
   /**
    * 送礼任务数据
    */
   public function getSendGiftLevel($type=1)
   {
        $res = DB::connection($this->activityConnection)->table($this->send_gift_level_log)
            ->select(DB::raw('COUNT(level = 1 or null) as firstLevel,COUNT(level=2 or null) as sencondLevel,COUNT(level=3 or null) as thirdLevel,COUNT(level=4 or null) as FourLevel,COUNT(level=5 or null) as fiveLevel,COUNT(DISTINCT send_userid) as all_user,create_time'))
            ->where('type',$type)->groupBy('create_time')->get();
            
        return $res;
   }
}